package com.fs.ibox.dao.impl;

import com.fs.ibox.dao.IdCollectionDao;
import com.fs.ibox.pojo.IdCollection;
import com.fs.ibox.pojo.User;
import com.fs.ibox.util.JDBCUtil;
import com.fs.ibox.vo.MarketCollection;
import com.fs.ibox.vo.SaleVO;
import com.fs.ibox.vo.UserCollection;

import java.util.List;

/**
 * Author:HOU
 * date:2022/11/15  15:31
 * 案例:
 **/
public class IdCollectionDaoimpl implements IdCollectionDao {
    @Override
    public List<UserCollection> queryUserCollection(User user) {
//        String sql = "SELECT cname FROM tb_idcollection WHERE username=?";
        String sql = "SELECT a.cname,b.releasenum,b.cimg,residual num FROM tb_idcollection a join tb_collection b ON a.cname=b.cname WHERE username=? AND cstate=2";

        return JDBCUtil.executeQuery(sql,UserCollection.class,user.getUserName());
    }


    @Override
    public Integer queryNumIncollection(User user) {
        return null;
    }


    /**
     * 展示用户正在售出的藏品
     * @param username
     * @return
     */
    @Override
    public List<SaleVO> queryCnameAndCidAndPriceAndcimg(String username) {
        String sql = "SELECT a.cname,cid,a.price,cimg FROM tb_idcollection a JOIN tb_collection b ON a.cname=b.cname WHERE username=? AND cstate=1";
        return JDBCUtil.executeQuery(sql, SaleVO.class, username);
    }

    @Override
    public int updatecstateByusernameAndCid(String cname, String cid) {
        String sql = "UPDATE tb_idcollection SET cstate = 2 WHERE cname=? AND cid=?";

        return JDBCUtil.executeUpdate(sql,cname,cid);
    }

    @Override
    public void addIdcollection(IdCollection idCollection) {
        String sql="INSERT INTO tb_idcollection (username,cname,cid,cstate,price) VALUES (?,?,?,2,?)";
        int i = JDBCUtil.executeUpdate(sql, idCollection.getUsername(), idCollection.getCname(), idCollection.getCid(), idCollection.getPrice());
    }
//改变状态为2
    @Override
    public void updateState(String username, String user, String cname, String cid) {



        String sql="UPDATE tb_idcollection SET cstate = 2 WHERE username = ? AND cname = ? AND cid = ?";
        JDBCUtil.executeUpdate(sql, username, cname, cid);
        String sql0 = "UPDATE tb_idcollection SET username = ? WHERE username = ? AND cname = ? AND cid = ?";
        JDBCUtil.executeUpdate(sql0, user, username,cname, cid);
    }

    @Override
    public List<MarketCollection> queryMarketCollections(String username) {
        String sql = "SELECT a.id, a.cname, cid, releasenum, a.price, cimg, startdate, enddate, content, a.username, residual FROM tb_idcollection a JOIN tb_collection b on a.cname = b.cname AND cstate = 1 AND a.username != ?";
        return JDBCUtil.executeQuery(sql, MarketCollection.class, username);
    }
    @Override
    public MarketCollection queryMarketById(Integer id) {
        String sql = "SELECT a.id, a.cname, cid, releasenum, a.price, cimg, startdate, enddate, content, a.username, residual FROM tb_idcollection a JOIN tb_collection b on a.cname = b.cname AND cstate = 1 AND a.id =  ?";
        return JDBCUtil.executeQuery(sql, MarketCollection.class, id).get(0);
    }


    @Override
    public IdCollection queryByName(String name) {
        String sql = "SELECT * FROM tb_idcollection WHERE state = 1 AND cname = ?";
        List<IdCollection> idCollections = JDBCUtil.executeQuery(sql, IdCollection.class, name);
        return idCollections.get(0);
    }

    @Override
    public List<IdCollection> queryAll() {
        String sql = "SELECT * FROM tb_idcollection WHERE cstate = 1";


        return JDBCUtil.executeQuery(sql, IdCollection.class);
    }


}
